﻿using System.Collections;
using System.Web.Mvc;
using WFramework;

namespace MiniWeb.Web.Controllers.Uploads
{
    public class UploadsController : BaseController
    {
      

       
        public ActionResult SaveSystemImage()
        {
            var hash = new Hashtable();

            if (Request.Files.Count == 0 || Request.Files[0] == null || Request.Files[0].ContentLength == 0)
            {
                hash["error"] = 1;
                hash["message"] = "上传的图片不能为空";
                var result = Json(hash, JsonRequestBehavior.AllowGet);
                result.ContentType = "text/html";
                return result;
            }

            string errMsg, savePath;
            if (!UploadService.UploadSystemImage(Request.Files[0], out errMsg, out savePath))
            {
                hash["error"] = 1;
                hash["message"] = errMsg;
                var result = Json(hash, JsonRequestBehavior.AllowGet);
                result.ContentType = "text/html";
                return result;
            }
            else
            {
                hash["error"] = 0;
                hash["url"] = savePath;
                var result = Json(hash, JsonRequestBehavior.AllowGet);
                result.ContentType = "text/html";
                return result;
            }
        }

        //[HttpPost]
        //public ActionResult SaveGoodsImage()
        //{
        //    var hash = new Hashtable();
        //    if (Request.Files.Count == 0 || Request.Files[0] == null || Request.Files[0].ContentLength == 0)
        //    {
        //        hash["error"] = 1;
        //        hash["message"] = "上传的图片不能为空";
        //        return Json(hash, JsonRequestBehavior.AllowGet);
        //    }

        //    string errMsg, savePath;
        //    if (!UploadService.UploadGoodsImage(Request.Files[0], out errMsg, out savePath))
        //    {
        //        hash["error"] = 1;
        //        hash["message"] = errMsg;
        //        return Json(hash, JsonRequestBehavior.AllowGet);
        //    }
        //    else
        //    {
        //        var log = new GoodsImgUploadLog
        //                      {
        //                          ImgUrl = savePath,
        //                          CreateDate = DateTime.Now,
        //                      };
        //        _goodsImgUploadLogTask.Add(log);
        //        hash["error"] = 0;
        //        hash["url"] = savePath;
        //        return Json(hash, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //public ActionResult GoodsImageList(int pageIndex = 1, int pageSize = 8)
        //{
        //    var pagedList = _goodsImgUploadLogTask.GetPagedList(pageIndex, pageSize);
        //    var items = pagedList.Select(EntityMapper.Map<GoodsImgUploadLog, GoodsImgUploadLogModel>).ToList();
        //    var model = new PagedList<GoodsImgUploadLogModel>(items, pageIndex, pageSize, pagedList.TotalItemCount);
        //    return View(model);
        //}

        //[HttpPost]
        //public ActionResult UploadGoodsImg()
        //{
        //    var hash = new Hashtable();
        //    if (Request.Files.Count == 0 || Request.Files[0] == null || Request.Files[0].ContentLength == 0)
        //        return AlertMsg("请选择要上传的图片", Request.UrlReferrer.PathAndQuery);

        //    string errMsg, savePath;
        //    if (!UploadService.UploadGoodsImage(Request.Files[0], out savePath, out errMsg))
        //        return AlertMsg(errMsg, Request.UrlReferrer.PathAndQuery);
        //    else
        //    {
        //        var log = new GoodsImgUploadLog
        //        {
        //            ImgUrl = savePath,
        //            CreateDate = DateTime.Now,
        //        };
        //        _goodsImgUploadLogTask.Add(log);
        //        return AlertMsg("上传成功", Request.UrlReferrer.PathAndQuery);
        //    }
        //}

        [HttpPost]
        public ActionResult UploadFile()
        {
            if (Request.Files.Count == 0 || Request.Files[0] == null || Request.Files[0].ContentLength == 0)
                return AlertMsg("请选择要上传的文件", Request.UrlReferrer.PathAndQuery);

            string errMsg, savePath;
            if (!UploadService.UploadFile(Request.Files[0], out savePath, out errMsg))
                return AlertMsg(errMsg, Request.UrlReferrer.PathAndQuery);
            else
            {
                
                return AlertMsg("上传成功", Request.UrlReferrer.PathAndQuery);
            }

        }

        //public ActionResult FileList(string fileName, int pageIndex = 1, int pageSize = 10)
        //{
        //    var pagedList = _fileUploadLogTask.GetPagedList(fileName, pageIndex, pageSize);
        //    var model = pagedList.Select(EntityMapper.Map<FileUploadLog, FileUploadLogModel>)
        //        .ToPagedList(pageIndex, pageSize, pagedList.TotalItemCount);
        //    return View(model);
        //}
    }
}